package com.example.logindb.sevices;

import com.example.logindb.controller.HelpController;
import com.example.logindb.entity.Help;
import com.example.logindb.repository.HelpRepository;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

@Service
public class HelpServiceImpl implements HelpService {

    private HelpRepository helpRepository;

    public HelpServiceImpl(HelpRepository helpRepository) {
        this.helpRepository = helpRepository;

    }

    @Override
    public void blindWaitforHelp(Help help) {
        helpRepository.save(help);
    }

    @Override
    public List<Help> findBlindByLoc(Double lat, Double lon) {
        Double dist= 0.2;
        Double minlat = lat - dist;
        Double maxlat = lat + dist;
        Double minlon = lon - dist;
        Double maxlon = lon + dist;
        return helpRepository.findBlindMessage(minlat,maxlat,minlon,maxlon);
    }

    @Override
    public Help findAllByAid(Integer aid) {
        return helpRepository.findAllByAid(aid);
    }

    @Override
    public void updateEnd(Help help) {
        Help help0 = helpRepository.findAllByAid(help.getAid());
        help.setVid(help0.getVid());
        help.setLon(help0.getLon());
        help.setLat(help0.getLat());
        help.setAddress(help0.getAddress());
        help.setStatus(2);//结束帮助
        help.setBid(help0.getBid());
        help.setBtime(help0.getBtime());
        help.setStime(help0.getStime());
        helpRepository.save(help);

    }

    @Override
    public void updateblind(Help help) {
        Help help0 = helpRepository.findAllByAid(help.getAid());
        help.setLon(help0.getLon());
        help.setLat(help0.getLat());
        help.setBid(help0.getBid());
        help.setBtime(help0.getBtime());
        help.setStatus(1);//开始帮助
        help.setAddress(help0.getAddress());
        helpRepository.save(help);
    }

    @Override
    public List<Help> findByBid(Integer bid) {

        return helpRepository.findAllByBid(bid);

    }

    @Override
    public List<Help> findByVid(Integer vid) {
        return helpRepository.findAllByVid(vid);
    }

    @Override
    public List<Integer> getCountByVid() {
        List<Integer> list = new ArrayList<>();
        int overtime = helpRepository.findAllByVid(-1).size();
        int waiting = helpRepository.findAllByVid(0).size();
        int completed = helpRepository.findAll().size()-waiting-overtime;
        list.add(overtime);
        list.add(waiting);
        list.add(completed);
        return list;
    }

    @Override
    public Integer dayDoneHelp(Timestamp startTime) {
        return helpRepository.findRecentMessagesWithVidMinusOne(startTime).size();
    }

    @Override
    public Integer dayOverHelp(Timestamp startTime) {
        return helpRepository.findRecentMessagesOvertime(startTime).size();
    }

    //根据经纬度求两点直线距离
    @Override
    public Float getDistance(double lon1, double lat1,double lon2, double lat2) {
        final double R = 6371; // Earth's radius in kilometers

        double phi1 = Math.toRadians(lat1);
        double phi2 = Math.toRadians(lat2);
        double deltaPhi = Math.toRadians(lat2 - lat1);
        double deltaLambda = Math.toRadians(lon2 - lon1);

        double a = Math.sin(deltaPhi / 2) * Math.sin(deltaPhi / 2)
                + Math.cos(phi1) * Math.cos(phi2)
                * Math.sin(deltaLambda / 2) * Math.sin(deltaLambda / 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

        // 四舍五入到小数点后两位
        double distance = R * c;
        distance = Math.round(distance * 100.0) / 100.0;
        return (float) distance; // returns distance in kilometers
    }

    @Override
    public List<Help> findByBidDone(Integer bid) {
        return helpRepository.findAllByBidDone(bid);
    }

    @Override
    public List<Help> findByBVidDone(Integer vid) {
        return helpRepository.findAllByVidDone(vid);
    }

    @Override
    public Help findStatus1AllByBid(int bid) {
        return helpRepository.findAllByVidAndStatus(bid);
    }


}